package com.apptest.apptest;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class IntentActivity extends AppCompatActivity {

    private EditText mEditPhone;
    private EditText mEditPwd;
    private TextView mTxtBackResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initViews();
        setupListener();
    }


    private void initViews() {
        mEditPhone = findViewById(R.id.et_phone);
        mEditPwd = findViewById(R.id.et_pwd);
        mTxtBackResult = findViewById(R.id.txt_back_result);
    }

    private void setupListener() {
        findViewById(R.id.btn_login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = mEditPhone.getText().toString().trim();
                String pwd = mEditPwd.getText().toString().trim();

                if (TextUtils.isEmpty(phone) || TextUtils.isEmpty(pwd)) {
                    Toast.makeText(IntentActivity.this, "请输入手机号或密码", Toast.LENGTH_SHORT).show();
                    return;
                }


                SecondActivity.launch(IntentActivity.this, phone, pwd);

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == RESULT_OK) {
            mTxtBackResult.setText("返回值为："+data.getStringExtra("EXTRA_RESULT"));
        }
    }

    public static void launch(Context context) {
        Intent intent=new Intent(context,IntentActivity.class);
        context.startActivity(intent);
    }
}
